package org.telegram.bot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.ruoyi.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.telegram.bot.domain.BotPublicBlock;
import org.telegram.bot.mapper.BotPublicBlockMapper;
import org.telegram.bot.service.IBotProhibitedWordsService;
import org.telegram.bot.service.IBotPublicBlockService;

import jakarta.annotation.Resource;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公共违禁词Service业务层处理
 *
 * @author wyx
 * @date 2024-11-27
 */
@Slf4j
@Service
public class BotPublicBlockServiceImpl implements IBotPublicBlockService
{
    @Resource
    private BotPublicBlockMapper botPublicBlockMapper;

    @Resource
    private IBotProhibitedWordsService botProhibitedWordsService;
    /**
     * 查询公共违禁词
     *
     * @param id 公共违禁词主键
     * @return 公共违禁词
     */
    @Override
    public BotPublicBlock selectBotPublicBlockById(Long id)
    {
        return botPublicBlockMapper.selectById(id);
    }

    /**
     * 查询公共违禁词列表
     *
     * @param botPublicBlock 公共违禁词
     * @return 公共违禁词
     */
    @Override
    public List<BotPublicBlock> selectBotPublicBlockList(BotPublicBlock botPublicBlock)
    {
        return botPublicBlockMapper.selectBotPublicBlockList(botPublicBlock);
    }

    /**
     * 新增公共违禁词
     *
     * @param botPublicBlock 公共违禁词
     * @return 结果
     */
    @Override
    public int insertBotPublicBlock(BotPublicBlock botPublicBlock)
    {
        Long l = botPublicBlockMapper.selectCount(new LambdaQueryWrapper<BotPublicBlock>().eq(BotPublicBlock::getWord, botPublicBlock.getWord()));
        if(SqlHelper.retBool(l)){
            throw new ServiceException("违禁词已存在");
        }
        return botPublicBlockMapper.insert(botPublicBlock);
    }

    /**
     * 修改公共违禁词
     *
     * @param botPublicBlock 公共违禁词
     * @return 结果
     */
    @Override
    public int updateBotPublicBlock(BotPublicBlock botPublicBlock)
    {
        Long l = botPublicBlockMapper.selectCount(new LambdaQueryWrapper<BotPublicBlock>().ne(BotPublicBlock::getId,botPublicBlock.getId()).eq(BotPublicBlock::getWord, botPublicBlock.getWord()));
        if(SqlHelper.retBool(l)){
            throw new ServiceException("违禁词已存在");
        }
        return botPublicBlockMapper.updateById(botPublicBlock);
    }

    /**
     * 批量删除公共违禁词
     *
     * @param ids 需要删除的公共违禁词主键
     * @return 结果
     */
    @Override
    public int deleteBotPublicBlockByIds(Long[] ids)
    {
        return botPublicBlockMapper.deleteByIds(Arrays.asList(ids));
    }

    /**
     * 删除公共违禁词信息
     *
     * @param id 公共违禁词主键
     * @return 结果
     */
    @Override
    public int deleteBotPublicBlockById(Long id)
    {
        return botPublicBlockMapper.deleteById(id);
    }
    
    @Override
    public String importToChat(Long chatId, Long[] ids) {
        List<String> collect = botPublicBlockMapper.selectBatchIds(Arrays.asList(ids)).stream().map(BotPublicBlock::getWord).collect(Collectors.toList());
        String words = String.join(",", collect);
        return botProhibitedWordsService.insertBotProhibitedWords(chatId,words).replaceAll("\n","<br>");
    }
}
